package com.cyjt.operation.ui;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.cyjt.operation.R;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.InspectionTimeInfo;
import com.cyjt.operation.view.ActionBarLayout;

public class MyOperationActivity extends Activity implements OnClickListener {
	/** 个人资料按键区 */
	private RelativeLayout relative_personal_data;
	/** 我负责的区域按键区 */
	private RelativeLayout relative_my_area;
	/** 我的工作时段按键区 */
	private RelativeLayout relative_my_work_time;

	private LocationClient locationClient = null;
	private static final int UPDATE_TIME = 180000;
	private static int LOCATION_COUTNS = 0;
	private List<InspectionTimeInfo> inspectionTimeInfo;
	private List<InspectionTimeInfo> inspectionLocLat;
	private InspectionTimeInfo inspectionInfo = new InspectionTimeInfo();
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_INSPECTION_TIME_SUCCESS:
				inspectionTimeInfo = (List<InspectionTimeInfo>) msg.obj;
				if (inspectionTimeInfo != null && inspectionTimeInfo.size() > 0) {
					inspectionInfo.setTollerId(inspectionTimeInfo.get(0).getId() + "");
					inspectionInfo.setDevice_no(inspectionTimeInfo.get(0).getNfcCode() + "");
					inspectionInfo.setAreaId(inspectionTimeInfo.get(0).getAreaId());
				}

				break;
			case HandlerMessageCodes.SUBMIT_INTSPECTION_TIME_SUCCESS:
				break;
			case HandlerMessageCodes.SUBMIT_INTSPECTION_TIME_FAILED:

				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_operation);
		initView();
		initLoc();
		AppContext.getInstance().getVolleyTools().fetchInspectionTimeInfo(handler,
				AppContext.getInstance().getUser().getId() + "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

	}

	/**
	 * 初始化定位系统
	 */
	private void initLoc() {
		locationClient = new LocationClient(this);
		// 设置定位条件
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true); // 是否打开GPS
		option.setCoorType("bd09ll"); // 设置返回值的坐标类型。
		option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级
		option.setProdName("LocationDemo"); // 设置产品线名称。强烈建议您使用自定义的产品线名称，方便我们以后为您提供更高效准确的定位服务。
		option.setScanSpan(UPDATE_TIME); // 设置定时定位的时间间隔。单位毫秒
		locationClient.setLocOption(option);

		// 注册位置监听器
		locationClient.registerLocationListener(new BDLocationListener() {

			@Override
			public void onReceiveLocation(BDLocation location) {
				if (location == null)
					return;
				StringBuffer sb = new StringBuffer(256);
				sb.append("Time:");
				sb.append(location.getTime());
				sb.append("\nError code : ");
				sb.append(location.getLocType());
				sb.append("\nLatitude : ");
				sb.append(location.getLatitude());
				sb.append("\nLontitude : ");
				sb.append(location.getLongitude());
				sb.append("\nRadius : ");
				sb.append(location.getRadius());
				if (location.getLocType() == BDLocation.TypeGpsLocation) {
					sb.append("\nSpeed : ");
					sb.append(location.getSpeed());
					sb.append("\nSatellite : ");
					sb.append(location.getSatelliteNumber());
				} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
					sb.append("\nAddress : ");
					sb.append(location.getAddrStr());
				}
				LOCATION_COUTNS++;
				sb.append("\n检查位置更新次数：");
				sb.append(String.valueOf(LOCATION_COUTNS));
				// Log.i("demo", "获取经纬度=" + sb.toString());
				inspectionInfo.setLat(location.getLatitude());
				inspectionInfo.setLon(location.getLongitude());
				AppContext.getInstance().getVolleyTools().submitInspectionTimeInfo(handler, inspectionInfo);

			}

			@Override
			public void onReceivePoi(BDLocation location) {

			}

		});
	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		ActionBarLayout.setTitle("我的运维");
		relative_personal_data = (RelativeLayout) findViewById(R.id.relative_personal_data);
		relative_personal_data.setOnClickListener(this);
		relative_my_area = (RelativeLayout) findViewById(R.id.relative_my_area);
		relative_my_area.setOnClickListener(this);
		relative_my_work_time = (RelativeLayout) findViewById(R.id.relative_my_work_time);
		relative_my_work_time.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO 控件的点击事件
		switch (v.getId()) {
		case R.id.relative_personal_data:
			// TODO 跳转到我的资料的Activity
			startActivity(new Intent(this, PersonalDataActivity.class));
			break;
		case R.id.relative_my_area:
			// TODO 跳转到巡检区域的Activity
			startActivity(new Intent(this, InspectionAreaActivity.class).putExtra("inspectionTimeInfo",
					(Serializable) inspectionTimeInfo));
			break;
		case R.id.relative_my_work_time:
			// TODO 跳转到排班记录查询的Activity
			startActivity(new Intent(this, QueryShiftRecordActivity.class).putExtra("inspectionTimeInfo",
					(Serializable) inspectionTimeInfo));
			break;

		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (locationClient != null && locationClient.isStarted()) {
			locationClient.stop();
			locationClient = null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
		locationClient.start();
		/*
		 * 当所设的整数值大于等于1000（ms）时，定位SDK内部使用定时定位模式。调用requestLocation(
		 * )后，每隔设定的时间，定位SDK就会进行一次定位。如果定位SDK根据定位依据发现位置没有发生变化，就不会发起网络请求，
		 * 返回上一次定位的结果；如果发现位置改变，就进行网络请求进行定位，得到新的定位结果。
		 * 定时定位时，调用一次requestLocation，会定时监听到定位结果。
		 */
		locationClient.requestLocation();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

}
